package com.bit.spring.controller;

import com.bit.spring.entity.MessageInfo;
import com.bit.spring.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RequestMapping("/message")
@RestController
public class MessageController {

    @Autowired
    private MessageService messageService;

//    private List<MessageInfo> messageInfoList = new ArrayList<>();

    @PostMapping("/publish")
    public Boolean publish(@RequestBody MessageInfo messageInfo){
        System.out.println("接收到参数, messaage:" + messageInfo);
        //1. 参数校验    2. 存储信息
        if (!StringUtils.hasLength(messageInfo.getFrom())
                || !StringUtils.hasLength(messageInfo.getTo())
                || !StringUtils.hasLength(messageInfo.getMessage())) {
            System.out.println("参数不规范");
            return false;
        }
        Integer result= messageService.insertMessage(messageInfo);
        return result==1;
    }

    @GetMapping("/getList")
    public List<MessageInfo> getList(){
        return messageService.getList();
    }
}
